package com.sskj.mine.account;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.account.data.AccountBean;
import com.sskj.mine.bean.AccountChoose;
import com.sskj.mine.bean.MineAccount;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.invite.data.CommonBean;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/03
 */
public class AccountManagerActivity extends BaseActivity<AccountManagerPresenter> {

    @BindView(R2.id.account_list)
    RecyclerView accountList;
    @BindView(R2.id.right_tv)
    TextView rightTv;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private MVCCoolHelper<List<MineAccount.Account>> mvcCoolHelper;
    private SlimAdapter<MineAccount.Account> accountAdapter;

    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    private int p = 1;
    private int size = 10;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_account_manager;
    }

    @Override
    public AccountManagerPresenter getPresenter() {
        return new AccountManagerPresenter();
    }

    @Override
    public void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_accountManagerActivity1_j));
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        rightTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.mine_icon_add_white, 0);
        ClickUtil.click(rightTv, () -> AddAccountActivity.start(this));
        accountList.setLayoutManager(new LinearLayoutManager(this));
        accountAdapter = SlimAdapter.create().register(R.layout.mine_item_account, new SlimInjector<MineAccount.Account>() {
            @Override
            public void onInject(MineAccount.Account commonBean, IViewInjector iViewInjector, List<Object> list) {
                iViewInjector.imageCircle(R.id.user_head, TextUtils.isEmpty(commonBean.getUpic()) ? getResources().getDrawable(R.mipmap.mine_user_head) : commonBean.getUpic())
                        .text(R.id.user_name, TextUtils.isEmpty(commonBean.getNickname()) ? "----" : commonBean.getNickname())
                        .text(R.id.user_id, commonBean.getAccount())
                        .clicked(R.id.select, view -> {
                            mPresenter.chooseAccount(commonBean.getUser_id());
                        });
                String s = SPUtil.get(SPConfig.ACCOUNT, "");
                iViewInjector.visibility(R.id.select, s.equals(commonBean.getAccount()) ? View.INVISIBLE : View.VISIBLE);
            }
        }).attachTo(accountList).updateData(new ArrayList());
        ModelRx2DataSource<MineAccount.Account> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<MineAccount.Account>)
                        page -> mPresenter.getAccount(p, size), size);
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(accountAdapter);
        mvcCoolHelper.refresh();
    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AccountManagerActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mvcCoolHelper != null) {
            mvcCoolHelper.refresh();
        }
    }

    //切换账户
    public void updateToken(AccountChoose data) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(SPConfig.ACCOUNT, data.getAccount());
        httpHeaders.put(SPConfig.TOKEN, data.getToken());
        SPUtil.clear();
        SPUtil.put(SPConfig.ACCOUNT, data.getAccount());
        SPUtil.put(SPConfig.TOKEN, data.getToken());
        SPUtil.put(SPConfig.LOGIN, true);
        OkGo.getInstance().addCommonHeaders(new HttpHeaders());
        OkGo.getInstance().getCommonHeaders().clear();
        OkGo.getInstance().addCommonHeaders(httpHeaders);
        userViewModel.update();
        RxBus.getDefault().send(RxBusCode.CHANGE_QUAN);
        finish();
    }
}
